import axios from 'axios'
//import {useRouter} from "vue-router"
import { loading,clearToast } from './vant';
import Cookies from './auth'
import router from '@/router'

//const router = useRouter()

// create an axios instance
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_API_URL, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})

// request拦截器
service.interceptors.request.use(config => {
  const authStr = router.currentRoute._value.params.auth

  if(!config.unLoading){
    loading()
  }
  
  if(authStr){
    config.headers['Authtxt'] = authStr 
  }

  const token = Cookies.get('token')
  if (token) {
		config.headers['Authorization'] = token 
	}
  return config
}, error => {
  Promise.reject(error)
})

// 响应拦截
service.interceptors.response.use(config => {
  clearToast()
  return config
}, error => {
  Promise.reject(error)
})

export default service
